package com.epoint.androidphone.mobileoa.ui.schedule;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.epoint.androidmobile.bizlogic.schedule.model.ServerRcModel;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ControlsHelp;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.date.DateUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;
import com.epoint.androidmobile.bizlogic.schedule.task.AddScheduleTask;
import com.epoint.androidmobile.bizlogic.schedule.task.EditScheduleTask;
/**
 * @author liyc
 * @time 2013-5-6 下午4:21:37
 * @annomation 
 */
public class ScheduleAddView extends SuperPhonePage {

	Button btAdd,btEdit,btKssj,btJssj;
	Spinner spZyx;
	String[] zyxInfo = new String[]{"无","低","高"};
	EditText etZt,etDd,etNr;
	ServerRcModel sr;

	long scheduleAddTaskId,scheduleEditTaskId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.scheduleaddview, "日程添加");
		etZt = (EditText) findViewById(R.id.etZt);
		etDd = (EditText) findViewById(R.id.etDd);
		etNr = (EditText) findViewById(R.id.etNr);
		btAdd = (Button) findViewById(R.id.btAdd);
		btAdd.setOnClickListener(this);
		btEdit = (Button) findViewById(R.id.btEdit);
		btEdit.setOnClickListener(this);
		btKssj = (Button) findViewById(R.id.btKssj);
		btKssj.setOnClickListener(this);
		btJssj = (Button) findViewById(R.id.btJssj);
		btJssj.setOnClickListener(this);
		spZyx = (Spinner) findViewById(R.id.spZyx);

		ArrayAdapter<String> zyxAdapter = ControlsHelp.getSPAdapter(this);
		for(String s:zyxInfo)
		{
			zyxAdapter.add(s);
		}
		spZyx.setAdapter(zyxAdapter);

		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);

		int nowMinutes = cal.get(Calendar.MINUTE);
		if(nowMinutes>=0&&nowMinutes<30)
		{
			cal.set(Calendar.MINUTE, 30);
		}
		else
		{
			cal.set(Calendar.MINUTE, 0);
			cal.add(Calendar.HOUR_OF_DAY, 1);
		}

		btKssj.setText(DateUtil.convertDate(cal.getTime(), "yyyy-MM-dd HH:mm"));

		cal.add(Calendar.MINUTE, 30);

		btJssj.setText(DateUtil.convertDate(cal.getTime(), "yyyy-MM-dd HH:mm"));

		int operateFlag = getIntent().getIntExtra("operateflag", 0);
		if(operateFlag==1)
		{
			btEdit.setVisibility(View.GONE);
		}
		else if(operateFlag==2)
		{
			btAdd.setVisibility(View.GONE);
			getTvTitle().setText("修改日程");
			sr = (ServerRcModel) getIntent().getSerializableExtra("model");
			etZt.setText(sr.Subject);
			etDd.setText(sr.Location);
			btKssj.setText(sr.FromDateTime);
			btJssj.setText(sr.ToDateTime);
			etNr.setText(sr.ActivityDescription);

			if(sr.Importance.equals("1"))
			{
				spZyx.setSelection(0);
			}
			else if(sr.Importance.equals("0"))
			{
				spZyx.setSelection(1);
			}
			else if(sr.Importance.equals("2"))
			{
				spZyx.setSelection(2);
			}
		}
		else{
			findViewById(R.id.llOperate).setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v==btKssj||v==btJssj)
		{
			AlertUtil.chooseDateAndTime(this,v);
		}
		else if(v==btAdd)
		{
			Date ksDate = DateUtil.convertString2Date(btKssj.getText().toString(), "yyyy-MM-dd HH:mm");
			Date jsDate = DateUtil.convertString2Date(btJssj.getText().toString(), "yyyy-MM-dd HH:mm");
			if(ksDate.getTime()>jsDate.getTime())
			{
				ToastUtil.toastWorning(this, "开始时间需要在结束时间之前!");
				return;
			}

			HashMap<String, Object> param = getPassMap();
			param.put("Subject", etZt.getText().toString().trim().equals("")?"无主题":etZt.getText().toString());
			param.put("Location", etDd.getText().toString());
			param.put("ActivityDescription", etNr.getText().toString());
			String importance = "1";
			if(spZyx.getSelectedItemPosition()==1)
			{
				importance = "0";
			}
			else if(spZyx.getSelectedItemPosition()==2)
			{
				importance = "2";
			}
			param.put("Importance", importance);
			param.put("FromDateTime", btKssj.getText().toString());
			param.put("ToDateTime", btJssj.getText().toString());

			scheduleAddTaskId = new AddScheduleTask(this, param).startTask();

		}
		else if(v==btEdit)
		{
			Date ksDate = DateUtil.convertString2Date(btKssj.getText().toString(), "yyyy-MM-dd HH:mm");
			Date jsDate = DateUtil.convertString2Date(btJssj.getText().toString(), "yyyy-MM-dd HH:mm");
			if(ksDate.getTime()>jsDate.getTime())
			{
				ToastUtil.toastWorning(this, "开始时间需要在结束时间之前!");
				return;
			}

			HashMap<String, Object> param = getPassMap();
			param.put("ActivityID", sr.ActivityID);
			param.put("Subject", etZt.getText().toString().trim().equals("")?"无主题":etZt.getText().toString());
			param.put("Location", etDd.getText().toString());
			param.put("ActivityDescription", etNr.getText().toString());
			String importance = "1";
			if(spZyx.getSelectedItemPosition()==1)
			{
				importance = "0";
			}
			else if(spZyx.getSelectedItemPosition()==2)
			{
				importance = "2";
			}
			param.put("Importance", importance);
			param.put("FromDateTime", btKssj.getText().toString());
			param.put("ToDateTime", btJssj.getText().toString());
			scheduleEditTaskId = new EditScheduleTask(this, param).startTask();
		}
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==scheduleAddTaskId)
		{
			if(validateXML(obj))
			{
				Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
		else if(taskId==scheduleEditTaskId)
		{
			if(validateXML(obj))
			{
				Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}
}
